home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / StorUtil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  12.5 KB  |  458 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        StorUtil.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Craig Carper
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <4>     6/24/96    RA        1355839: SOM_TRY => TRY
  13.          <3>     3/26/96    CC        1333083: RemoveDataInterchangeProperties
  14.                                     removes kODPropOriginalID property.
  15.          <2>     3/15/96    CC        1316917: Changed
  16.                                     RemoveDataInterchangeProperties parameter
  17.                                     name to "keepProxyProperties".
  18.  
  19.     To Do:
  20. */
  21.  
  22. /*
  23.     File:        StorUtil.cpp
  24.  
  25.     Contains:    Utilities for working with ODStorageUnits.
  26.  
  27.     Owned by:    Vincent Lo
  28.  
  29.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  30.  
  31.     
  32.     In Progress:
  33.         
  34. */
  35.  
  36. #ifndef _STORUTIL_
  37. #include <StorUtil.h>
  38. #endif
  39.  
  40. #ifndef SOM_ODDraft_xh
  41. #include <Draft.xh>
  42. #endif
  43.  
  44. #ifndef SOM_ODSession_xh
  45. #include <ODSessn.xh>
  46. #endif
  47.  
  48. #ifndef SOM_ODStorageSystem_xh
  49. #include <ODStor.xh>
  50. #endif
  51.  
  52. #ifndef SOM_ODDocument_xh
  53. #include <Document.xh>
  54. #endif
  55.  
  56. #ifndef SOM_ODContainer_xh
  57. #include <ODCtr.xh>
  58. #endif
  59.  
  60. #ifndef SOM_ODStorageUnit_xh
  61. #include <StorageU.xh>
  62. #endif
  63.  
  64. #ifndef SOM_ODStorageUnitView_xh
  65. #include <SUView.xh>
  66. #endif
  67.  
  68. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  69. #include <StdDefs.xh>
  70. #endif
  71.  
  72. #ifndef SOM_Module_OpenDoc_StdProps_defined
  73. #include <StdProps.xh>
  74. #endif
  75.  
  76. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  77. #include "StdTypes.xh"
  78. #endif
  79.  
  80. #ifndef SOM_ODTranslation_xh
  81. #include <Translt.xh>
  82. #endif
  83.  
  84. #ifndef _BARRAY_
  85. #include <BArray.h>
  86. #endif
  87.  
  88. #ifndef _STDTYPIO_
  89. #include <StdTypIO.h>
  90. #endif
  91.  
  92. #ifndef _PLFMFILE_
  93. #include <PlfmFile.h>
  94. #endif
  95.  
  96. #ifndef _EXCEPT_
  97. #include <Except.h>
  98. #endif
  99.  
  100. #ifndef _ODMEMORY_
  101. #include <ODMemory.h>
  102. #endif
  103.  
  104. #ifndef __TEXTEDIT__
  105. #include <TextEdit.h>
  106. #endif
  107.  
  108. #ifndef _TEMPOBJ_
  109. #include <TempObj.h>
  110. #endif
  111.  
  112. //==============================================================================
  113. // ODSUAddPropValue
  114. //==============================================================================
  115.  
  116. void        ODSUAddPropValue(Environment* ev,
  117.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  118. {
  119.     su->AddProperty(ev, prop)->AddValue(ev, val);
  120. }
  121.  
  122. //==============================================================================
  123. // ODSUForceFocus
  124. //==============================================================================
  125.  
  126. void        ODSUForceFocus(Environment* ev, 
  127.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  128. {
  129.     if (prop != kODNULL) {
  130.         if (su->Exists(ev, prop, kODNULL, 0) == kODFalse)
  131.             su->AddProperty(ev, prop);
  132.         else
  133.             su->Focus(ev, prop, kODPosUndefined, kODNULL, 0, kODPosUndefined);
  134.     }
  135.     if (val != kODNULL) { 
  136.         if (su->Exists(ev, prop, val, 0) == kODFalse)
  137.             su->AddValue(ev, val);
  138.         else
  139.             su->Focus(ev, prop, kODPosSame, val, 0, kODPosUndefined);
  140.     }
  141.     // else; // Presumably the caller has already focussed the su somewhere,
  142.              // perhaps midstream. -TC
  143. }
  144.  
  145. //==============================================================================
  146. // ODSUExistsThenFocus
  147. //==============================================================================
  148.  
  149. ODBoolean        ODSUExistsThenFocus(Environment* ev, 
  150.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  151. {
  152.     if (prop == kODNULL && val == kODNULL)
  153.         return kODTrue;
  154.     // else; // Presumably the caller has already focussed the su somewhere,
  155.              // perhaps midstream. Property:kODNULL && ValueType:kODNULL always 'exist'. -TC
  156.              
  157.     if (su->Exists(ev, prop, val, 0))
  158.     {
  159.         if ( val )
  160.             su->Focus(ev,  prop, kODPosSame, val, 0, kODPosSame);
  161.         else
  162.             su->Focus(ev,  prop, kODPosSame, kODNULL, 0, kODPosAll);
  163.         return kODTrue;    
  164.     }
  165.     else
  166.     {
  167.         return kODFalse;
  168.     }
  169. }
  170.  
  171. //==============================================================================
  172. // ODSURemoveProperty
  173. //==============================================================================
  174.  
  175. void ODSURemoveProperty(Environment* ev, ODStorageUnit* su, ODPropertyName prop)
  176. {
  177.     if ( ODSUExistsThenFocus(ev, su, prop, kODNULL) )
  178.         su->Remove(ev);
  179. }
  180.  
  181. //----------------------------------------------------------------------------------------
  182. // GetPlatformFileFromContainer
  183. //----------------------------------------------------------------------------------------
  184. PlatformFile*    GetPlatformFileFromContainer(Environment* ev, ODContainer* container)
  185. {
  186.     ODByteArray    ba = container->GetID(ev);
  187.     PlatformFile*    file = new PlatformFile();
  188.     file->Specify((ODFileSpec*) ba._buffer);
  189.     ODDisposePtr(ba._buffer);
  190.     
  191.     return file;
  192. }
  193.  
  194. //----------------------------------------------------------------------------------------
  195. // GetODFileSpecFromContainer
  196. //----------------------------------------------------------------------------------------
  197. ODFileSpec GetODFileSpecFromContainer(Environment* ev, ODContainer* container)
  198. {
  199.     ODByteArray    ba = container->GetID(ev);
  200.     ODFileSpec documentSpec = *((ODFileSpec*) ba._buffer);
  201.     ODDisposePtr(ba._buffer); // DMc dispose when done
  202.  
  203.     return documentSpec;
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // CreateFileContainer
  208. //----------------------------------------------------------------------------------------
  209. ODContainer*    CreateFileContainer(Environment* ev, ODSession* session, ODFileSpec* fsSpec)
  210. {
  211.     ODByteArray*    ba = CreateByteArray(fsSpec, sizeof(short) + sizeof(long) + fsSpec->name[0] + 1);
  212.     ODContainer*    newContainer = session->GetStorageSystem(ev)->
  213.         CreateContainer(ev,kODDefaultFileContainer /*file->GetContainerType()*/,ba);
  214.     DisposeByteArray(ba);
  215.     
  216.     return newContainer;
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // GetFileContainer
  221. //----------------------------------------------------------------------------------------
  222. ODContainer*    GetFileContainer(Environment* ev, ODSession* session, ODFileSpec* fsSpec)
  223. {
  224.     ODByteArray*    ba = CreateByteArray(fsSpec, sizeof(short) + sizeof(long) + fsSpec->name[0] + 1);
  225.     ODContainer*    newContainer = session->GetStorageSystem(ev)->
  226.         AcquireContainer(ev,kODDefaultFileContainer /*file->GetContainerType()*/,ba);
  227.     DisposeByteArray(ba);
  228.     
  229.     return newContainer;
  230. }
  231.     
  232. //----------------------------------------------------------------------------------------
  233. // CreateMemoryContainer
  234. //----------------------------------------------------------------------------------------
  235. ODContainer* CreateMemoryContainer(Environment* ev,
  236.                 ODSession* session,
  237.                 ODHandle handle,
  238.                 ODContainerType containerType)
  239. {
  240.     ODByteArray* ba;
  241.     TRY
  242.         ODLockHandle(handle);
  243.         ba = CreateByteArray(&handle, sizeof(ODHandle));
  244.         ODUnlockHandle(handle);
  245.     CATCH_ALL
  246.         ODUnlockHandle(handle);
  247.         RERAISE;
  248.     ENDTRY
  249.  
  250.     ODContainer* newContainer = session->GetStorageSystem(ev)->
  251.         CreateContainer(ev, containerType, ba);
  252.     DisposeByteArray(ba);
  253.     
  254.     return newContainer;
  255. }
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // GetMemoryContainer
  259. //----------------------------------------------------------------------------------------
  260. ODContainer* GetMemoryContainer(Environment* ev, 
  261.                 ODSession* session,
  262.                 ODHandle handle,
  263.                 ODContainerType containerType)
  264. {
  265.     ODByteArray* ba;
  266.     TRY
  267.         ODLockHandle(handle);
  268.         ba = CreateByteArray(&handle, sizeof(ODHandle));
  269.         ODUnlockHandle(handle);
  270.     CATCH_ALL
  271.         ODUnlockHandle(handle);
  272.         RERAISE;
  273.     ENDTRY
  274.  
  275.     ODContainer* newContainer = session->GetStorageSystem(ev)->
  276.         AcquireContainer(ev, containerType, ba);
  277.     DisposeByteArray(ba);
  278.     
  279.     return newContainer;
  280. }
  281.  
  282. //----------------------------------------------------------------------------------------
  283. // StorageUnitGetValue
  284. //----------------------------------------------------------------------------------------
  285. ODULong    StorageUnitGetValue(ODStorageUnit* su,
  286.                             Environment* ev,
  287.                             ODULong    size,
  288.                             ODPtr buffer)
  289. {
  290.     ODByteArray    ba;    
  291.     ODULong bytesRead = su->GetValue(ev, size, &ba);
  292.     ODBlockMove(ba._buffer, buffer, bytesRead);
  293.     ODDisposePtr(ba._buffer);
  294.  
  295.     return bytesRead;
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. // StorageUnitViewGetValue
  300. //----------------------------------------------------------------------------------------
  301. ODULong    StorageUnitViewGetValue(ODStorageUnitView* suv,
  302.                             Environment* ev,
  303.                             ODULong    size,
  304.                             ODPtr buffer)
  305. {
  306.     ODByteArray    ba;
  307.     
  308.     ODULong bytesRead = suv->GetValue(ev, size, &ba);
  309.     ODBlockMove(ba._buffer, buffer, ba._length);
  310.     ODDisposePtr(ba._buffer);
  311.     
  312.     return bytesRead;
  313. }
  314.  
  315.  
  316. //----------------------------------------------------------------------------------------
  317. // StorageUnitSetValue
  318. //----------------------------------------------------------------------------------------
  319. void    StorageUnitSetValue(ODStorageUnit* su,
  320.                             Environment* ev,
  321.                             ODULong    size,
  322.                             const void *buffer)
  323. {
  324.     ODByteArray ba;
  325.     ba._length = size;
  326.     ba._maximum = size;
  327.     ba._buffer = (octet*) buffer;
  328.     su->SetValue(ev, &ba);
  329. }
  330.  
  331. //----------------------------------------------------------------------------------------
  332. // StorageUnitViewSetValue
  333. //----------------------------------------------------------------------------------------
  334. void    StorageUnitViewSetValue(ODStorageUnitView* suv,
  335.                                 Environment* ev,
  336.                                 ODULong    size,
  337.                                 const void *buffer)
  338. {
  339.     ODByteArray ba;
  340.     ba._length = size;
  341.     ba._maximum = size;
  342.     ba._buffer = (octet*) buffer;
  343.     suv->SetValue(ev, &ba);
  344. }
  345.  
  346. //----------------------------------------------------------------------------------------
  347. // StorageUnitSetPromiseValue
  348. //----------------------------------------------------------------------------------------
  349. void    StorageUnitSetPromiseValue(ODStorageUnit* su,
  350.                                     Environment* ev,
  351.                                     ODValueType valueType,
  352.                                     ODULong offset,
  353.                                     ODULong    size,
  354.                                     const void *buffer,
  355.                                     ODPart *sourcePart)
  356. {
  357.     ODByteArray ba;
  358.     ba._length = size;
  359.     ba._maximum = size;
  360.     ba._buffer = (octet*) buffer;
  361.     su->SetPromiseValue(ev, valueType, offset, &ba, sourcePart);
  362. }
  363.  
  364. //----------------------------------------------------------------------------------------
  365. // StorageUnitGetStylFromStyledText
  366. //----------------------------------------------------------------------------------------
  367. ODBoolean StorageUnitGetStylFromStyledText(ODStorageUnit* su,
  368.                             Environment* ev,
  369.                             ODULong* size,
  370.                             ODPtr* styl)
  371. {
  372.     ODBoolean    result = kODFalse;
  373.     ODULong        stxtSize;
  374.     ODULong        stylSize;
  375.     ODUShort    scrpNStyles;
  376.     ODType        applestxt = kODNULL;
  377.  
  378.     const ODPlatformType kODScrapType_stxt = 0x73747874;    // 'stxt'
  379.  
  380.     ODVolatile(applestxt);
  381.     ODVolatile(result);
  382.     
  383.     TRY
  384.  
  385.         ODTranslation* translation = su->GetSession(ev)->GetTranslation(ev);
  386.         applestxt = translation->GetISOTypeFromPlatformType(ev, kODScrapType_stxt, kODPlatformDataType);
  387.             
  388.         if (ODSUExistsThenFocus(ev, su, kODPropContents, applestxt) )
  389.         {
  390.             stxtSize = su->GetSize(ev);
  391.             if ( stxtSize >= sizeof(ODUShort) )
  392.             {
  393.                 StorageUnitGetValue(su, ev, sizeof(ODUShort), &scrpNStyles);
  394.                 stylSize = (scrpNStyles * sizeof(ScrpSTElement)) + sizeof(ODUShort);
  395.                 if ( stxtSize >= stylSize )
  396.                 {
  397.                     *styl = ODNewPtr(stylSize);
  398.                     *size = stylSize;
  399.                     su->SetOffset(ev, 0);
  400.                     StorageUnitGetValue(su, ev, stylSize, *styl);
  401.                     result = kODTrue;
  402.                 }
  403.             }
  404.         }
  405.         
  406.         delete applestxt;
  407.     
  408.     CATCH_ALL
  409.             if ( applestxt )
  410.                 delete applestxt;
  411.             result = kODFalse;
  412.     ENDTRY
  413.  
  414.     return result;
  415. }
  416.  
  417. //------------------------------------------------------------------------------
  418. // GetOriginalCloneKind
  419. //------------------------------------------------------------------------------
  420.  
  421. ODCloneKind GetOriginalCloneKind(Environment* ev, ODDraft* draft)
  422. {
  423.     // If content was put on the clipboard without cloning, there will be no
  424.     // kODPropOriginalCloneKind property in the draft's preferences storage
  425.     // unit.  Assume the original operation was a copy in this case. Since
  426.     // no links cannot be placed on the clipboard directly, there won't be
  427.     // links needing fixup on a paste.
  428.  
  429.     TempODStorageUnit draftProperties = draft->AcquireDraftProperties(ev);
  430.     ODCloneKind cloneKind = ODGetULongProp(ev, draftProperties, kODPropOriginalCloneKind, kODULong);
  431.     
  432.     if ((ODULong)(cloneKind) != 0)
  433.         return cloneKind;
  434.     else
  435.         return kODCloneCopy;
  436. }
  437.  
  438. //------------------------------------------------------------------------------
  439. // RemoveDataInterchangeProperties
  440. //------------------------------------------------------------------------------
  441.  
  442. void RemoveDataInterchangeProperties (Environment* ev,
  443.         ODStorageUnit*    su,
  444.         ODBoolean        keepProxyProperties)
  445. {
  446.     ODSURemoveProperty(ev, su, kODPropLinkSpec);
  447.     ODSURemoveProperty(ev, su, kODPropMouseDownOffset);
  448.     ODSURemoveProperty(ev, su, kODPropCloneKindUsed);
  449.     ODSURemoveProperty(ev, su, kODPropOriginalID);
  450.     if ( !keepProxyProperties )
  451.     {
  452.         ODSURemoveProperty(ev, su, kODPropProxyContents);
  453.         ODSURemoveProperty(ev, su, kODPropContentFrame);
  454.         ODSURemoveProperty(ev, su, kODPropSuggestedFrameShape);
  455.     }
  456. }
  457.  
  458.